const todoState = {
    list: JSON.parse(localStorage.getItem("todo")) || [],//源数据
    copyList: JSON.parse(localStorage.getItem("todo")) || []
}

export function todoReducer(state = todoState, action) {
    console.log(action)
    switch (action.type) {
        case "Add":
            var flag = false;
            state.list.forEach(item => {
                if (item.context === action.todo.context) {
                    flag = true;
                }
            })

            if (flag) {
                alert("不能重复添加同样的信息");
                return state;
            }

            state.list.push(action.todo);
            localStorage.setItem("todo", JSON.stringify(state.list))
            return {
                list: [...state.list],
                copyList: [...state.list]
            }
        case "changeStatus":
            state.list.forEach((item) => {
                if (item.context == action.todo.context) {
                    item.status = (action.checked ? "finished" : "Doing")
                }
            })
            localStorage.setItem("todo", JSON.stringify(state.list))
            return {
                list: [...state.list],
                copyList: [...state.list]
            }
        case "all":
            return {
                list: [...state.list],
                copyList: [...state.list]
            }
        case "doing":
            state.copyList = [];
            state.list.forEach((item) => {
                if (item.status === "Doing") {
                    state.copyList.push(item)
                }
            })

            return {
                list: state.list,
                copyList: [...state.copyList]
            }
        case "showFinish":
            state.copyList = [];
            state.list.forEach((item) => {
                if (item.status === "finished") {
                    state.copyList.push(item)
                }
            })
            return {
                list: state.list,
                copyList: [...state.copyList]
            }
        case "delete":
            var tempIndex1 = state.list.findIndex((item) => {
                return item.context === action.todo.context;
            })
            var tempIndex2 = state.copyList.findIndex((item) => {
                return item.context === action.todo.context;
            })

            if (tempIndex1 != -1) {
                state.list.splice(tempIndex1, 1);
            }

            if (tempIndex2 != -1) {
                state.copyList.splice(tempIndex2, 1);
            }
            localStorage.setItem("todo", JSON.stringify(state.list))
            return {
                list: [...state.list],
                copyList: [...state.copyList]
            }
        case "delAllMsg":
            state.list=[]
            state.copyList=[]
            
            localStorage.setItem("todo", JSON.stringify(state.list))
            return {
                list: [...state.list],
                copyList: [...state.list]
            }
        default:
            return state;
    }
}